package com.project.m.api.busi.db.service.hicatcard.user;

import com.project.m.api.busi.db.entity.hicatcard.card.TCard;
import com.project.m.api.busi.db.entity.hicatcard.card.TCardService;
import com.project.m.api.busi.db.entity.hicatcard.user.TUser;
import com.project.m.api.busi.db.entity.hicatcard.user.TUserCard;
import com.project.m.api.busi.db.entity.hicatcard.user.TUserCardRechargeNewUser;
import com.project.m.api.busi.db.entity.hicatcard.user.TUserCardService;
import com.project.m.api.busi.db.repo.card.CardRepo;
import com.project.m.api.busi.db.repo.card.CardServiceRepo;
import com.project.m.api.busi.db.repo.user.UserCardRechargeNewUserRepo;
import com.project.m.api.busi.db.repo.user.UserCardRepo;
import com.project.m.api.busi.db.repo.user.UserCardServiceRepo;
import com.project.m.api.busi.util.IdGenerator.IdGenerator;
import com.project.m.api.busi.util.TimeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Transactional
public class UserCardRechargeNewUserService {

    @Autowired
    UserCardRechargeNewUserRepo userCardRechargeNewUserRepo;

    @Autowired
    CardRepo cardRepo;

    @Autowired
    CardServiceRepo cardServiceRepo;

    @Autowired
    UserCardRepo userCardRepo;

    @Autowired
    UserCardServiceRepo userCardServiceRepo;

    public void saveUserCardRechargeNewUser(TUserCardRechargeNewUser userCardRechargeNewUser) {
        userCardRechargeNewUserRepo.save(userCardRechargeNewUser);
    }

    @Transactional(readOnly = true)
    public List<TUserCardRechargeNewUser> findByPhoneAndEnableAndStatus(String phone, Boolean enable, Integer status) {
        return userCardRechargeNewUserRepo.findByPhoneAndEnableAndStatus(phone, enable, status);
    }

    public void savePresentCard(List<TUserCardRechargeNewUser> userCardRechargeNewUserList, TUser user) {

        List<TUserCard> userCardList = new ArrayList<TUserCard>();
        List<TUserCardService> userCardServiceList = new ArrayList<TUserCardService>();
        for (TUserCardRechargeNewUser userCardRechargeNewUserItem : userCardRechargeNewUserList) {
            //查找卡记录
            TCard card = cardRepo.findByCardId(userCardRechargeNewUserItem.getCardId());
            if (card != null) {
                for (int i = 0; i < userCardRechargeNewUserItem.getNumber(); i++) {
                    //有增加记录，添加到t_user_card中
                    TUserCard userCard = new TUserCard();
                    userCard.setUserId(user.getUserId());
                    userCard.setCardId(card.getCardId());
                    userCard.setUserCardNumber(IdGenerator.generateOrderId(IdGenerator.ORDERID_PREFIX_USER_CARD, "00", "0000"));
                    userCard.setSource(TUserCard.SOURCE_COMPANY_PRESENT);
                    userCard.setSourceUserId(null);
                    userCard.setStatus(TUserCard.STATUS_ACTIVE);
                    userCard.setCreateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
                    userCard.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
                    userCard.setUserCardRechargeId(userCardRechargeNewUserItem.getUserCardRechargeId());
                    Integer effectiveDay = card.getEffectiveDay();
                    String endTime = TimeUtil.getFormatTime(TimeUtil.addDateNDay(new Date(), effectiveDay), TimeUtil.DATE_PATTERN_NOSEPARTOR);
                    endTime = StringUtils.substring(endTime, 0, endTime.length() - 6) + "235959";
                    userCard.setEndTime(endTime);
                    userCard.setIsNew(TUserCard.IS_NEW_TRUE);
                    userCard.setEnable(true);
                    userCardList.add(userCard);
                }
            }
            userCardRechargeNewUserItem.setUserId(user.getUserId());
            userCardRechargeNewUserItem.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
            userCardRechargeNewUserItem.setStatus(TUserCardRechargeNewUser.STATUS_GET);
        }

        //更新记录
        userCardList = userCardRepo.save(userCardList);
        for (TUserCard userCard : userCardList) {
            List<TCardService> cardServiceList = cardServiceRepo.findByCardId(userCard.getCardId());
            for (TCardService cardService : cardServiceList) {
                TUserCardService userCardService = new TUserCardService();
                userCardService.setUserCardId(userCard.getUserCardId());
                userCardService.setServiceId(cardService.getServiceId());
                userCardService.setServiceTime(cardService.getServiceTime());
                userCardService.setStatus(TCardService.STATUS_IN_USE);
                userCardService.setCreateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
                userCardService.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
                userCardService.setEnable(true);
                userCardServiceList.add(userCardService);
            }

        }
        //添加记录
        userCardServiceRepo.save(userCardServiceList);
        userCardRechargeNewUserRepo.save(userCardRechargeNewUserList);

    }
}
